package com.administrator.scoreManageSys.utils;

import com.administrator.scoreManageSys.system.SystemConstant;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 数字工具类
 *
 * @author Administrator
 */
public class NumberUtils {
    /**
     * 获取n位随机位数的字符串
     *
     * @param length 要获取的字符串长度
     * @return n 为随机数字符串
     */
    public static String getRandomString(int length) {

        String base = SystemConstant.LETTER+SystemConstant.NUMERAL;
        Random random = new Random();
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            stringBuffer.append(base.charAt(number));
        }
        return stringBuffer.toString();
    }

    /**
     * 返回随机产生的8位数 首字母为字母  后续为数字
     */
    public static String getRandomPassword(int len) {
        return makeRandomPassword(len);
    }

    /**
     * 产生8位随机数 首字母为字母  后续为数字
     */
    public static  String makeRandomPassword(int len) {
        char[] initials = SystemConstant.LETTER.toCharArray();
        char[] chars = SystemConstant.NUMERAL.toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < len; ++x) {
            if (x==0){
                sb.append(initials[r.nextInt(initials.length)]);
            }else {
                sb.append(chars[r.nextInt(chars.length)]);
            }
        }
        return sb.toString();
    }
    /**
     * 获取n位随机位数
     *
     * @param length 要获取的数字长度
     * @return n 为随机数字
     */
    public static String getRandomNumber(int length) {
        Random random = new Random();
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(SystemConstant.NUMERAL.length());
            stringBuffer.append(SystemConstant.NUMERAL.charAt(number));
        }
        return stringBuffer.toString();
    }
}
